package com.aqie.easy.doublePointer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 500. 键盘行
 */
public class FindWordsFromKeyboard {
    public static String[] findWords(String[] words) {
        List<String> res = new ArrayList<>();
        if (words == null) return null;
        String[] lines = new String[]{
                "qwertyuiop",
                "asdfghjkl",
                "zxcvbnm"
        };
        for (String word : words){
            if (judge(word.toLowerCase(),lines)){
                res.add(word);
            }
        }
        // return res.toArray(new String[res.size()]);
        return res.toArray(new String[0]);
    }

    private static boolean judge(String word, String[] lines){
        boolean ok = true;
        String find = null;

        // 先判断 word 在哪一行
        for (String line : lines){
            if (line.indexOf(word.charAt(0)) > -1){
                find = line;
                break;
            }
        }
        if (find == null){
            ok = false;
            return false;
        }

        // 判断word 字符的所有字符是否在同一行
        for(int i = 1; i < word.length(); i++){
            if (find.indexOf(word.charAt(i)) == -1){
                ok = false;
                break;
            }
        }
        return ok;
    }

    public static void main(String[] args) {
        String[] words = {"Hello", "Alaska", "Dad", "Peace"};
        System.out.println(Arrays.toString(findWords(words)));
    }
}
